package com.example.gokchinesefoodmapmcdev.controller;

import com.example.gokchinesefoodmapmcdev.entity.MerchantInfo;
import com.example.gokchinesefoodmapmcdev.service.MerchantService;
import com.example.gokchinesefoodmapmcdev.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/rest/app/good")
public class MerchantController {

    @Autowired
    private MerchantService merchantService;

    /**
     * 根据商户id查询商户信息，并返回部分商家数据
     * @param merchantId  商户id
     * @return
     */
    @PostMapping("/getMerchantInfo")
    public ResultData getMerchantInfo(@RequestBody Integer merchantId){
        MerchantInfo merchantInfo = merchantService.findMerchantInfoById(merchantId);
        Map<String, Object> map = new HashMap<>();
        map.put("merchantId", merchantInfo.getId());
        map.put("merchantName", merchantInfo.getMerchantName());
        map.put("merchantLogo", merchantInfo.getMerchantLogo());
        ResultData resultData = new ResultData(ResultData.OPERATION_SUCCESS, "", map);
        return resultData;
    }

    /**
     * 根据商户id，查询该商户的商品列表
     */
    @RequestMapping("/listByMerchantId")
    public ResultData getGoodsListByMerchantId(@RequestBody Map<String, Object> map){
        Integer merchantId = (Integer) map.get("merchantId");
        Integer pageNumber = (Integer) map.get("pageNumber");
        Integer pageSize = (Integer) map.get("pageSize");
        List<Map<String, Object>> result = merchantService.findGoodsListByMerchantId(merchantId, pageNumber, pageSize);
        if(result.isEmpty()){
            return ResultData.ok(ResultData.OPERATION_SUCCESS, "当前没有商品信息");
        }
        return new ResultData(ResultData.OPERATION_SUCCESS, "", result);
    }


}
